<?php

include '../sys_application.php';
include 'func/customerInfo.php';
include 'func/func.php';


$sysApps = new sysApps();
$sysApps->DBconnect();
	
$user = $_SESSION['user_MGM_id'];

$sql = "SELECT mu.id, mu.email, mu.url_name, mu.phone_no, mu.user_MGM_id, mu.fullname, mu.identity_no, mu.address1, mu.address2, mu.mobile_no, us.start_month, us.end_month
		FROM mgm_users mu
		LEFT JOIN user_subscription us 
		ON mu.id = us.user_id
		WHERE mu.sponsor_id = '$user'
		AND (us.end_month = (SELECT MAX(us1.end_month) 
							FROM user_subscription us1
							WHERE us1.user_id = mu.id)
	   		OR us.end_month IS NULL )
		GROUP BY mu.id 
		ORDER BY mu.id";

$result = mysql_query($sql);

$sql2 = "SELECT mu.id, mu.email, mu.url_name, mu.phone_no, mu.user_MGM_id, mu.fullname, mu.identity_no, mu.address1, mu.address2, mu.mobile_no, us.start_month, us.end_month
		FROM mgm_users mu
		LEFT JOIN user_subscription us 
		ON mu.id = us.user_id
		WHERE mu.user_MGM_id = '$user'
		AND (us.end_month = (SELECT MAX(us1.end_month) 
							FROM user_subscription us1
							WHERE us1.user_id = mu.id)
			OR us.end_month IS NULL )";

$result2 = mysql_query($sql2);

$allInfo = new retreaveInfo(0, 0, " ");
$allchild = array();
$childrend = 0;
$totalChild = 0;
$totalActive = 0;
$totalKey = 0;
$totalIncome=0;
$childrend = mysql_fetch_array($result);
$key = 2;

while($row = mysql_fetch_array($result))
{	
	$retreaveInfo = new retreaveInfo(0, 0, " ");
	$retreaveInfo->setTrees("<ul><li>".$row['url_name']."<ul>");
	$allInfo = calculateIncome($row['user_MGM_id'], $retreaveInfo);
	$totalChild = $totalChild + intval($allInfo->getTotalChild());
	$totalActive = $totalActive + intval($allInfo->getTotalActive());
// 	echo $row['user_MGM_id']."--> totalChild -- > ".$totalChild."<p>";
	$statusActive = checkStatus($row['start_month'],$row['end_month']);
	$obj=new CustomerInfo($row['id'], $row['email'], $row['url_name'], $row['phone_no'], $row['user_MGM_id'], $row['fullname'], $row['identity_no'], $row['address1'], $row['address2'], $row['mobile_no'], $allInfo->getTotalChild(), $allInfo->getTotalActive(), $statusActive);
	$allchild[] = $obj;
	$dummy = $allInfo->getTrees()."</ul></li></ul>";
	$allInfo->setTrees($dummy);
	
	// count totalKey and totalIncome
	if($key != 0)
	{
		$totalKey = $totalKey + intval($allInfo->getTotalActive());
		$key--;
	}
	else
	{
		$totalIncome = $totalIncome + intval($allInfo->getTotalActive());
	}
}

$userProfileDB = mysql_fetch_array($result2);
$parentStatus = checkStatus($userProfileDB['start_month'],$userProfileDB['end_month']);
$userProfile=new CustomerInfo($userProfileDB['id'], $userProfileDB['email'], $userProfileDB['url_name'], $userProfileDB['phone_no'], $userProfileDB['user_MGM_id'], $userProfileDB['fullname'], $userProfileDB['identity_no'], $userProfileDB['address1'], $userProfileDB['address2'], $userProfileDB['mobile_no'], $totalChild, $totalActive, $parentStatus);

mysqli_close($con);
?>
<link href="css/layout.css" rel="stylesheet" type="text/css" />
<div id="childInfo"><h2>Income Tree</h2><br/>
	<table>
		<tr>
			<?php 
				$count = 0;
				$numberOfRow = 3;
				$dummyCount = $numberOfRow;
				foreach ($allchild as $child) 
				{
					if($count < 2)
					{
						if(($count % $numberOfRow) == 0 && $count != 0)
						{
							echo "</tr><tr>";
						}
						echo "<td align='center'><a href='key-tree?child=".$child->getMgmId()."'><div ".parentBlockStyle($child->getStatus()).">";
						echo $child->getUserName() . "<br/>" . $child->getMgmId() . "<br/><br/> Total = " . $child->getChild(). "<br/>Active = " . $child->getChildActive();
						echo "</div></a></td>";
						$count++;
					}
					else
					{
						if(($dummyCount % $numberOfRow) == 0)
						{
							echo "</tr><tr>";
						}
						echo "<td align='center'><a href='key-tree?child=".$child->getMgmId()."'><div ".parentBlockStyle($child->getStatus()).">";
						echo $child->getUserName() . "<br/>" . $child->getMgmId() . "<br/><br/> Total = " . $child->getChild(). "<br/>Active = " . $child->getChildActive();
						echo "</div></a></td>";
						$dummyCount++;
					}
					
				}
				$baki = $numberOfRow - ($dummyCount % $numberOfRow);
				for($i = 0; $i < $baki; $i++)
				{
// 						echo "<td><div class='block-level emphasize-dark'>&nbsp;</div></td>";
					echo "<td>&nbsp;</td>"; 
				}
			
			?>
		
		</tr>
	</table>
</div>

